import 'package:get/get.dart';
import 'package:getx_scaffold/common/utils/toast_util.dart';
import 'package:oramemo/core/repositories/default_api.dart';
import 'package:oramemo/entity/base_entity.dart';
import 'package:oramemo/entity/custom_model_list_entity.dart';
import 'package:oramemo/entity/requst/device_put_entity.dart';
import 'package:oramemo/pages/home/home_logic.dart';

import 'custom_recode_state.dart';

class CustomRecodeLogic extends GetxController {
  final CustomRecodeState state = CustomRecodeState();
  bool isEdit = false;

  @override
  void onInit() {
    load();
    super.onInit();
  }

  void selectRecord(int index) {
    state.selectedIndex.value = index;
    update();
  }

  void setEdit() {
    isEdit = true;
    update();
  }

  //提交
  void submit() async {
    CustomModelListRows data = state.list[state.selectedIndex.value];
    DevicePutEntity devicePutEntity = DevicePutEntity()
      ..id = data.deviceId
      ..customModelId = data.id
      ..deviceConfig = data.modelConfig;
    Get.back(result: devicePutEntity.toString());
  }

  void load() async {
    final res =
        await DefaultApi().oraCustomModelListGet<CustomModelListEntity>();
    state.list.clear();
    if (res?.rows?.isNotEmpty == true) {
      state.list.addAll(res!.rows!);
    }
    update();
  }
}
